#!/usr/bin/env python3
# -*- coding:utf-8 -*-
## author : cypro666
## date   : 2015.08.01
''' unit test '''
import os, sys
import unittest, time

dataDir = "../data"

testLR = 'python3 -O regression.py \
--train_file {path}/train.csv \
--label_file {path}/label.csv \
--testing_file {path}/testing.csv \
--results_file {path}/results.csv \
--log_file {path}/skit.log \
--schema=lasso \
--nonzero_coefs=6 \
--tol=0.0001 \
--eps=0.0005 \
--alpha=0.01 \
--max_iter=1000 \
--normalize=0'

testKNN = 'python3 -O knn.py \
--train_file {path}/train.csv \
--label_file {path}/label.csv \
--testing_file {path}/testing.csv \
--results_file {path}/results.csv \
--log_file {path}/skit.log \
--num_neighbors=6 \
--leaf_size=32 \
--algorithm=ball_tree \
--weights=distance'

testBayes = 'python3 -O bayes.py \
--train_file {path}/train.csv \
--label_file {path}/label.csv \
--testing_file {path}/testing.csv \
--results_file {path}/results.csv \
--log_file {path}/skit.log \
--schema=m \
--alpha=0.01 \
--binarize=0.5 \
--normalize=0'

testDTree = 'python3 -O dtree.py \
--train_file {path}/train.csv \
--label_file {path}/label.csv \
--testing_file {path}/testing.csv \
--results_file {path}/results.csv \
--log_file {path}/skit.log \
--normalize=0 \
--min_samples_split=2 \
--min_samples_leaf=2 \
--min_weight_fraction_leaf=0.1 \
--max_features=auto'

testSVM = 'python3 -O svm.py \
--train_file {path}/train.csv \
--label_file {path}/label.csv \
--testing_file {path}/testing.csv \
--results_file {path}/results.csv \
--log_file {path}/skit.log \
--normalize=1 \
--cache_size=256 \
--kernel=rbf \
--probability=1 \
--gamma=1.00 \
--coef0=1e-6 \
--tol=0.0001 \
--max_iter=500 \
--degree=3'

testGMM = 'python3 -O gmm.py \
--train_file {path}/train.csv \
--label_file {path}/label.csv \
--testing_file {path}/testing.csv \
--results_file {path}/results.csv \
--log_file {path}/skit.log \
--normalize=1 \
--n_components=4 \
--n_init=4 \
--n_iter=200 \
--min_covar=0.0001 \
--tol=0.0002'

testApriori = 'python3 -O apriori.py \
--train_file {path}/train.csv \
--results_file {path}/results.csv \
--log_file {path}/skit.log \
--min_support=0.25 \
--min_confidence=0.65'

testNMF = 'python3 -O nmf.py \
--train_file {path}/train.csv \
--results_file {path}/results.csv \
--log_file {path}/skit.log \
--normalize=0 \
--init=random \
--n_components=5 \
--max_iter=200 \
--nls_max_iter=3000 \
--tol=0.003'

testKMeans = 'python3 -O kmeans.py \
--train_file {path}/train.csv \
--results_file {path}/results.csv \
--log_file {path}/skit.log \
--n_clusters=4 \
--n_init=4 \
--tol=0.0001 \
--max_iter=1000'

testAdaBoost = 'python3 -O adaboost.py \
--train_file {path}/train.csv \
--label_file {path}/label.csv \
--testing_file {path}/testing.csv \
--results_file {path}/results.csv \
--log_file {path}/skit.log \
--normalize=0 \
--losser=square \
--target=classify \
--estimator=dtree \
--learning_rate=0.25 \
--n_estimators=4'


class TestAll(unittest.TestCase):
    ''' unit tester for iteralgos '''
    def command(self, cmd):
        err = os.system(cmd.format(path=dataDir))
        if err: 
            print(os.strerror(err))
            return False
        return True
    
    def setUp(self):
        print(' ' + self.id().split('.')[-1], file=sys.stderr, end=' ')
    
    def tearDown(self):
        pass
    
    def test_LR(self):
        self.assertTrue(self.command(testLR))
    
    def test_KNN(self):
        self.assertTrue(self.command(testKNN))
    
    def test_Bayes(self):
        self.assertTrue(self.command(testBayes))
    
    def test_DTree(self):
        self.assertTrue(self.command(testDTree))
    
    def test_SVM(self):
        self.assertTrue(self.command(testSVM))
    
    def test_GMM(self):
        self.assertTrue(self.command(testGMM))

    def test_Apriori(self):
        self.assertTrue(self.command(testApriori))
    
    def test_NMF(self):
        self.assertTrue(self.command(testNMF))
    
    def test_KMeans(self):
        self.assertTrue(self.command(testKMeans))
     
    def test_AdaBoost(self):
        self.assertTrue(self.command(testAdaBoost))


if __name__ == '__main__':
    ''' test '''
    unittest.main()


